package com.aysafety.iotdataplatformweb.controller.elevator;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aysafety.core.common.Common;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.elevator.ElevatorService;

@RestController
@RequestMapping("/api/elevatorMonitor/realMonitor/elevator/")
public class ElevatorController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ElevatorService elevatorService;

    @RequestMapping("/realTime")
    public Map<String, Object> getRealTime(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("deviceId")) {
                return this.formatReturnData("缺少参数");
            }
            String deviceId = params.get("deviceId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "100";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "100";
            int size = Integer.parseInt(sizeStr);
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            String firstStartRow = params.containsKey("firstStartRow") ? params.get("firstStartRow").toString() : "";
            data.put("total", elevatorService.countRealTime(deviceId, startTime, endTime));
            result =
                Common.getData(elevatorService.queryRealTime(deviceId, startTime, endTime, page, size + 1, startRow),
                    size, startRow, firstStartRow);
            data.put("list", result.get("list"));
            data.put("firstStartRow", result.get("firstStartRow"));
            data.put("lastStartRow", result.get("lastStartRow"));
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("ElevatorController-getRealTime:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("ElevatorController-getRealTime:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }

    @RequestMapping("/alarm")
    public Map<String, Object> getAlarm(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("deviceId")) {
                return this.formatReturnData("缺少参数");
            }
            String deviceId = params.get("deviceId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "100";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "100";
            int size = Integer.parseInt(sizeStr);
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            String firstStartRow = params.containsKey("firstStartRow") ? params.get("firstStartRow").toString() : "";
            data.put("total", elevatorService.countAlarm(deviceId, startTime, endTime));
            result = Common.getData(elevatorService.queryAlarm(deviceId, startTime, endTime, page, size + 1, startRow),
                size, startRow, firstStartRow);
            data.put("list", result.get("list"));
            data.put("firstStartRow", result.get("firstStartRow"));
            data.put("lastStartRow", result.get("lastStartRow"));
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("ElevatorController-getAlarm:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("ElevatorController-getAlarm:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }
}
